﻿Imports System
Imports System.Web

''' <summary>
''' Methods to read and write to asp.net sessions.
''' </summary>
Public Class SessionUtil

#Region "Private constructor"

	Private Sub New()
		' Can't create instance!
	End Sub

#End Region

#Region "Get"

	''' <summary>
	''' Gets the session value and return as String.
	''' </summary>
	''' <returns>Session as String.</returns>
	Public Shared Function GetSession(ByVal name As String, ByVal defaultValue As String) As String

		Dim obj As Object = GetSession(name)

		If obj Is Nothing Then
			Return defaultValue
		End If

		Return obj.ToString()

	End Function

	''' <summary>
	''' Gets the session value and return as DateTime.
	''' </summary>
	''' <returns>Session as DateTime.</returns>
	Public Shared Function GetSession(ByVal name As String, ByVal defaultValue As DateTime) As DateTime

		Dim obj As Object = GetSession(name)

		If obj Is Nothing Then
			Return defaultValue
		End If

		Return Convert.ToDateTime(obj)

	End Function

	''' <summary>
	''' Gets the session value and return as Int32.
	''' </summary>
	''' <returns>Session as Int32.</returns>
	Public Shared Function GetSession(ByVal name As String, ByVal defaultValue As Int32) As Int32

		Dim obj As Object = GetSession(name)

		If obj Is Nothing Then
			Return defaultValue
		End If

		Return Convert.ToInt32(obj)

	End Function

	''' <summary>
	''' Gets the session value and return as Boolean.
	''' </summary>
	''' <returns>Session as Boolean.</returns>
	Public Shared Function GetSession(ByVal name As String, ByVal defaultValue As Boolean) As Boolean

		Dim obj As Object = GetSession(name)

		If obj Is Nothing Then
			Return defaultValue
		End If

		Return Convert.ToBoolean(obj)

	End Function

	''' <summary>
	''' Gets the session value and return as Object.
	''' </summary>
	''' <returns>Session as Object.</returns>
	Public Shared Function GetSession(ByVal name As String) As Object

		Return HttpContext.Current.Session(name)

	End Function

#End Region

#Region "Set"

	''' <summary>
	''' Saves the specified Object to a session and sets the expiry date.
	''' </summary>
	Public Shared Sub SetSession(ByVal name As String, ByVal value As Object)

		HttpContext.Current.Session(name) = value

	End Sub

#End Region

End Class